package com.wz.app.activity;

import com.wz.app.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class NewsItemActivity extends Activity implements OnClickListener,
		OnKeyListener {

	private WebView webview;
	private ImageView back, share;
	private String url,title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.news_item_activity);
		//获取intent传过来的数据
		url = this.getIntent().getStringExtra("url");
		title = this.getIntent().getStringExtra("title");
		
		System.out.println(url);
		webview = (WebView) findViewById(R.id.news_item_webView);
		back = (ImageView) findViewById(R.id.news_item_back_button);
		share = (ImageView) findViewById(R.id.news_item_share_button);
		back.setOnClickListener(this);
		share.setOnClickListener(this);

		webview.loadUrl(url);
		WebSettings settings = webview.getSettings();
		settings.setJavaScriptEnabled(true);
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		// 返回按钮
		case R.id.news_item_back_button:
			Intent intent = new Intent(NewsItemActivity.this, MainActivity.class);
			startActivity(intent);
			break;
		// 分享按钮
		case R.id.news_item_share_button:
			Intent sendIntent = new Intent();  
			sendIntent.setAction(Intent.ACTION_SEND);  
			sendIntent.putExtra(Intent.EXTRA_TEXT, title+" \n "+url);  
			sendIntent.setType("text/plain");  
			startActivity(sendIntent);  
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
			webview.goBack(); // goBack()表示返回WebView的上一页面
			return true;
		}
		return false;
	}

}
